package cn.wildfire.chat.app.extendsion.classes;

import android.content.Context;

import com.google.gson.Gson;
import com.sourcemuch.im.caiba.CaiBaChatApp;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Create by HEKE, 2019/8/27 9:23
 * TODO 后台返回数据响应实体对象
 */
public class ResponseEntity<T, R>{

    /**
     * 返回结果为集合类型
     */
    public static final int TYPE_ARRAY = 0x01;

    /**
     * 返回结果为对象类型
     */
    public static final int TYPE_OBJECT = 0x02;

    private Context context;
    private String result;

    private boolean isError;
    private String errorCode;
    public String message;

    private JSONArray contentJsonArray;
    private JSONObject contentJsonObject;

    /**
     * 返回数据类型
     */
    public int type = TYPE_OBJECT;

    /**
     * 结果对象的class
     * */
    protected Class resultClass;

    public ResponseEntity(String result) {
        this.result = result;
        this.context = CaiBaChatApp.getContext();
        parse();
    }

    public ResponseEntity(Context context, String result) {
        this.context = context;
        this.result = result;
        parse();
    }

    public ResponseEntity(String result, Class resultClass) {
        this.result = result;
        this.resultClass = resultClass;
        parse();
    }

    private void parse() {
        try {
            if (result.contains("code")) {
                JSONObject jsonObject = new JSONObject(result);
                errorCode = jsonObject.getString("code");
                isError = !errorCode.equals("0");
                message = jsonObject.getString("message");
                if (result.contains("result")) {
                    try {
                        contentJsonObject = jsonObject.getJSONObject("result");
                    } catch (Exception e) {
                    } finally {
                        try {
                            contentJsonArray = jsonObject.getJSONArray("result");
                        } catch (Exception e) {
                        } finally {
                            if (contentJsonObject != null) {
                                type = TYPE_OBJECT;
                            }
                            if (contentJsonArray != null) {
                                type = TYPE_ARRAY;
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public boolean isError() {
        return isError;
    }

    public String getErrorCode() {
        return errorCode;
    }

    public boolean isSuccess(){
        return errorCode!=null&&errorCode.equals("0");
    }

    public Context getContext() {
        return context;
    }

    /**
     * 获取ResultJson数据对象
     */
    public T getResult() {
        T t = null;
        switch (type) {
            case TYPE_ARRAY:
                t = (T) contentJsonArray;
                break;
            case TYPE_OBJECT:
                t = (T) contentJsonObject;
                break;
                default:break;
        }
        return t;
    }

    /**
     * 提供解析结果方法，共外部常态使用
     * @param classtype
     * @return
     */
    public R parse(Class classtype) {
        R r = null;
        try {
            T t = getResult();
            if (t == null)
                return null;
            Gson gson = new Gson();
            if (type == TYPE_ARRAY) {
                ArrayList<Object> objectArrayList = new ArrayList<>();
                JSONArray jsonArray = (JSONArray) t;
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject object = jsonArray.getJSONObject(i);
                    Object o = gson.fromJson(object.toString(), classtype);
                    objectArrayList.add(o);
                }
                r = (R) objectArrayList;
            } else {
                JSONObject object = (JSONObject) t;
                Object o = gson.fromJson(object.toString(), classtype);
                r = (R) o;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return r;
    }

    /**
     * 提供解析结果方法，共外部常态使用
     * @return
     */
    public R parseResult() {
        if(resultClass == null){
            return null;
        }
        R r = null;
        try {
            T t = getResult();
            if (t == null)
                return null;
            Gson gson = new Gson();
            if (type == TYPE_ARRAY) {
                ArrayList<Object> objectArrayList = new ArrayList<>();
                JSONArray jsonArray = (JSONArray) t;
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject object = jsonArray.getJSONObject(i);
                    Object o = gson.fromJson(object.toString(), resultClass);
                    objectArrayList.add(o);
                }
                r = (R) objectArrayList;
            } else {
                JSONObject object = (JSONObject) t;
                Object o = gson.fromJson(object.toString(), resultClass);
                r = (R) o;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return r;
    }

}
